﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WWF.Data;
using WWF.Contracts;
using WWF.Services.UniversityApplicationService;

namespace WWF.Services
{
    public partial class InterviewResultUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateApplicantInfo();
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            UpdateInterviewResult("Accepted");
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            UpdateInterviewResult("Rejected");
        }

        private void UpdateInterviewResult(string result)
        {
            UniversityApplicationServiceClient proxy = new UniversityApplicationServiceClient();
            try
            {
                InterviewResult interviewResult = new InterviewResult
                {
                    ApplicationID = GetAppIDFromRequest(),
                    Result = result
                };
                UpdateInterviewResultRequest request = new UpdateInterviewResultRequest(interviewResult);
                proxy.UpdateInterviewResult(interviewResult);
                proxy.Close();
                ShowInfoMessage("Interview result updated successfully!");
            }
            catch (Exception)
            {
                proxy.Abort();
                throw;
            }
        }

        private void PopulateApplicantInfo()
        {
            int appID = GetAppIDFromRequest();
            if (appID != -1)
            {
                using (UniversityApplicationEntities ctx = new UniversityApplicationEntities())
                {
                    Application app = (from a in ctx.Applications
                                       where a.ApplicationID == appID
                                       select a).FirstOrDefault();
                    if (app != null)
                    {
                        lblID.Text = app.ApplicantID;
                        lblName.Text = app.ApplicantName;
                    }
                    else
                    {
                        ShowInfoMessage("Invalid ApplicationID");
                    }
                }
            }
            else
            {
                ShowInfoMessage("Invalid ApplicationID");
            }
        }

        private int GetAppIDFromRequest()
        {
            string appIDArg = Request.QueryString["AppID"];

            if (!string.IsNullOrEmpty(appIDArg))
            {
                try
                {
                    return Convert.ToInt32(appIDArg);
                }
                catch (FormatException)
                {
                    return -1;
                }
            }
            else
                return -1;
        }
        private void ShowInfoMessage(string message)
        {
            pnlUpdateArea.Visible = false;
            pnlInfo.Visible = true;
            lblInfo.Text = message;
        }
    }
}